//
//  XKLEessenceItem.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/4.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLEessenceItem.h"
#import "NSDate+XKLConpareTime.h"
#import "XKLTopCommetItem.h"
#import "XKLUserItem.h"

@implementation XKLEessenceItem
{
    CGFloat _cellHeight;
}

+(NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"top_cmt":@"top_cmt[0]",
             @"ID":@"id"
             };
}

-(NSString *)created_at{
    
    //时间格式化
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //格式化格式
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转成为时间
    NSDate *createDate = [formatter dateFromString:_created_at];
    
    if (createDate.isYear) {
        if (createDate.isToday) {
            
            NSDateComponents *components = [createDate deltaFrom];
            
            if (components.hour>=1) {
                
                return [NSString stringWithFormat:@"%ld小时前",components.hour];
            }else if (components.minute<1){
                return @"刚刚";
            }else{
                
                return [NSString stringWithFormat:@"%ld分钟前",components.minute];
            }
            
            
        }else if (createDate.isYesterday){
            formatter.dateFormat = @"昨天 HH:mm:ss";
            return [formatter stringFromDate:createDate];
        }else{
            formatter.dateFormat = @"MM-dd HH:mm:ss";
            NSString *dateStr = [formatter stringFromDate:createDate];
            
            return dateStr;
        }
        
        
        
    }else{
        return _created_at;
    }
    
    
    
    return _created_at;
}


-(CGFloat)cellHeight{
    
    if (_cellHeight==0) {
       //帖子没有任何内容的时候的高度
        _cellHeight += XKLAllCellTopHeight + XKLAllCellMargin ;
        
        //计算正文的高度
        
        CGSize maxSize = CGSizeMake(ScreenW - 4 * XKLAllCellMargin, MAXFLOAT);
        CGFloat textHeight = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        _cellHeight += textHeight + XKLAllCellMargin;
        
        if (self.type == kXKLTopicTypePicture) {
            CGFloat imageW = ScreenW - 4 * XKLAllCellMargin;
            CGFloat imageH = imageW * self.height / self.width;
            self.bigPictureSize = CGSizeMake(imageW, imageH);
            //判断是否是会大图
            if (imageH >1000){
                self.isBigPicture = YES;
                imageH = 250 ;
                _frame = CGRectMake(XKLAllCellMargin, _cellHeight, imageW, imageH);
                _cellHeight += imageH + XKLAllCellMargin;
                
            }else{
                _frame = CGRectMake(XKLAllCellMargin, _cellHeight, imageW, imageH);
                _cellHeight += imageH + XKLAllCellMargin;
                
            }
            
        }else if (self.type == kXKLTopicTypeVideo || self.type == kXKLTopicTypeVoice){
            CGFloat imageW = ScreenW - 4 * XKLAllCellMargin;
            CGFloat imageH = imageW * self.height / self.width;
            _frame = CGRectMake(XKLAllCellMargin, _cellHeight, imageW, imageH);
            _cellHeight += imageH + XKLAllCellMargin;
        }
        
        if (self.top_cmt) {
            
            NSString *str = [NSString stringWithFormat:@"%@:%@",self.top_cmt.user.username,self.top_cmt.content];
            
            CGFloat commentTextHeight = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
            
            _cellHeight += XKLTopCommentHeight + XKLAllCellMargin + commentTextHeight;
        }
        _cellHeight += XKLAllCellDingHeight + XKLAllCellMargin;
    }
    
    return _cellHeight;
}
@end
